﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SQLite;

namespace GameScoring.DBModel
{

    public class DBDatabase
    {
        SQLiteAsyncConnection Database;
        public DBDatabase()
        {
        }
        public async Task Init()
        {
            if (Database is not null)
                return;

            Database = new SQLiteAsyncConnection(Constants.DatabasePath, Constants.Flags);
            await Database.CreateTableAsync<DBGameName>();
            await Database.CreateTableAsync<DBGameInfo>();
        }
        /// <summary>
        /// 获取链接
        /// </summary>
        /// <returns></returns>
        public SQLiteAsyncConnection GetDBConnection()
        {
            Database = new SQLiteAsyncConnection(Constants.DatabasePath, Constants.Flags);
            return Database;
        }

        //public async Task<List<TodoItem>> GetItemsAsync()
        //{
        //    await Init();
        //    return await Database.Table<TodoItem>().ToListAsync();
        //}

        //public async Task<List<TodoItem>> GetItemsNotDoneAsync()
        //{
        //    await Init();
        //    return await Database.Table<TodoItem>().Where(t => t.Done).ToListAsync();

        //    // SQL queries are also possible
        //    //return await Database.QueryAsync<TodoItem>("SELECT * FROM [TodoItem] WHERE [Done] = 0");
        //}

        //public async Task<TodoItem> GetItemAsync(int id)
        //{
        //    await Init();
        //    return await Database.Table<TodoItem>().Where(i => i.ID == id).FirstOrDefaultAsync();
        //}

        //public async Task<int> SaveItemAsync(TodoItem item)
        //{
        //    await Init();
        //    if (item.ID != 0)
        //    {
        //        return await Database.UpdateAsync(item);
        //    }
        //    else
        //    {
        //        return await Database.InsertAsync(item);
        //    }
        //}

        //public async Task<int> DeleteItemAsync(TodoItem item)
        //{
        //    await Init();
        //    return await Database.DeleteAsync(item);
        //}
    }
}
